<!--
 * @Descripttion: (退款单表/corecmsbillrefund)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2025-02-16)
 * @LastEditors: (admin)
 * @LastEditTime: (2025-02-16)
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" size="small" label-position="right" inline ref="queryForm" label-width="100px" v-show="showSearch" 
      @submit.native.prevent>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['corecmsbillrefund:add']" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" :disabled="single" v-hasPermi="['corecmsbillrefund:edit']" plain icon="el-icon-edit" size="mini" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['corecmsbillrefund:delete']" plain icon="el-icon-delete" size="mini" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据区域 -->
    <el-table :data="dataList" v-loading="loading" ref="table" border highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column prop="refundId" label="退款单ID" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="aftersalesId" label="售后单id" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="money" label="退款金额" align="center" />
      <el-table-column prop="userId" label="用户ID 关联user.id" align="center" />
      <el-table-column prop="sourceId" label="资源id，根据type不同而关联不同的表" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="type" label="资源类型1=订单,2充值单" align="center">
        <template slot-scope="scope">
          <dict-tag :options=" typeOptions" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column prop="paymentCode" label="退款支付类型编码 默认原路返回 关联支付单表支付编码" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="tradeNo" label="第三方平台交易流水号" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <dict-tag :options=" statusOptions" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column prop="memo" label="退款失败原因" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="createTime" label="创建时间" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="updateTime" label="更新时间" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="agentMoney" label="代理佣金" align="center" />
      <el-table-column prop="pId" label="代理ID" align="center" />

      <el-table-column label="操作" align="center" width="140">
        <template slot-scope="scope">
          <el-button size="mini" v-hasPermi="['corecmsbillrefund:edit']" type="success" icon="el-icon-edit" title="编辑" 
            @click="handleUpdate(scope.row)"></el-button>      
          <el-button size="mini" v-hasPermi="['corecmsbillrefund:delete']" type="danger" icon="el-icon-delete" title="删除" 
            @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination class="mt10" background :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="->,total, sizes, prev, pager, next, jumper" />

    <!-- 添加或修改退款单表对话框 -->
    <el-dialog :title="title" :lock-scroll="false" :visible.sync="open" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
        
          <el-col :lg="12">
            <el-form-item label="退款单ID" prop="refundId">
              <el-input-number v-model.number="form.refundId" controls-position="right" placeholder="请输入退款单ID" :disabled="title=='修改数据'"/>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="售后单id" prop="aftersalesId">
              <el-input v-model="form.aftersalesId" placeholder="请输入售后单id" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="退款金额" prop="money">
              <el-input v-model="form.money" placeholder="请输入退款金额" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item label="用户ID 关联user.id" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入用户ID 关联user.id" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="资源id，根据type不同而关联不同的表" prop="sourceId">
              <el-input v-model="form.sourceId" placeholder="请输入资源id，根据type不同而关联不同的表" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item label="资源类型1=订单,2充值单" prop="type">
              <el-select v-model="form.type" placeholder="请选择资源类型1=订单,2充值单">
                <el-option v-for="item in  typeOptions" :key="item.dictValue" :label="item.dictLabel" :value="parseInt(item.dictValue)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="退款支付类型编码 默认原路返回 关联支付单表支付编码" prop="paymentCode">
              <el-input v-model="form.paymentCode" placeholder="请输入退款支付类型编码 默认原路返回 关联支付单表支付编码" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="第三方平台交易流水号" prop="tradeNo">
              <el-input v-model="form.tradeNo" placeholder="请输入第三方平台交易流水号" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio v-for="item in statusOptions" :key="item.dictValue" :label="parseInt(item.dictValue)">{{item.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="退款失败原因" prop="memo">
              <el-input v-model="form.memo" placeholder="请输入退款失败原因" />
            </el-form-item>
          </el-col>



          <el-col :lg="12">
            <el-form-item label="代理佣金" prop="agentMoney">
              <el-input v-model="form.agentMoney" placeholder="请输入代理佣金" />
            </el-form-item>
          </el-col>
    
          <el-col :lg="12">
            <el-form-item label="代理ID" prop="pId">
              <el-input v-model="form.pId" placeholder="请输入代理ID" />
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="medium" @click="cancel">取 消</el-button>
        <el-button size="medium" type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { 
  listCorecmsbillrefund,
  addCorecmsbillrefund,
  delCorecmsbillrefund,
  updateCorecmsbillrefund,
  getCorecmsbillrefund,
} from '@/api/order/corecmsbillrefund.js';

export default {
  name: "corecmsbillrefund",
  data() {
    return {
      labelWidth: "100px",
      formLabelWidth:"100px",
      // 选中refundId数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sort: undefined,
        sortType: undefined,
      },
      // 弹出层标题
      title: "",
      // 操作类型 1、add 2、edit
      opertype: 0,
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      columns: [
        { index: 0, key: 'refundId', label: `退款单ID`, checked:  true  },
        { index: 1, key: 'aftersalesId', label: `售后单id`, checked:  true  },
        { index: 2, key: 'money', label: `退款金额`, checked:  true  },
        { index: 3, key: 'userId', label: `用户ID 关联user.id`, checked:  true  },
        { index: 4, key: 'sourceId', label: `资源id，根据type不同而关联不同的表`, checked:  true  },
        { index: 5, key: 'type', label: `资源类型1=订单,2充值单`, checked:  true  },
        { index: 6, key: 'paymentCode', label: `退款支付类型编码 默认原路返回 关联支付单表支付编码`, checked:  true  },
        { index: 7, key: 'tradeNo', label: `第三方平台交易流水号`, checked:  true  },
        { index: 8, key: 'status', label: `状态`, checked:  true  },
        { index: 9, key: 'memo', label: `退款失败原因`, checked:  false  },
        { index: 10, key: 'createTime', label: `创建时间`, checked:  false  },
        { index: 11, key: 'updateTime', label: `更新时间`, checked:  false  },
        { index: 12, key: 'agentMoney', label: `代理佣金`, checked:  false  },
        { index: 13, key: 'pId', label: `代理ID`, checked:  false  },
      ],
      // 资源类型1=订单,2充值单选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
typeOptions: [],
      // 状态选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
statusOptions: [],
      dataList: [],
      total: 0,
      rules: {
        refundId: [
          { required: true, message: "退款单ID不能为空", trigger: "blur" }
        ],
        aftersalesId: [
          { required: true, message: "售后单id不能为空", trigger: "blur" }
        ],
        money: [
          { required: true, message: "退款金额不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户ID 关联user.id不能为空", trigger: "blur" }
        ],
        sourceId: [
          { required: true, message: "资源id，根据type不同而关联不同的表不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "资源类型1=订单,2充值单不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        agentMoney: [
          { required: true, message: "代理佣金不能为空", trigger: "blur" }
        ],
      },
    };
  },
  created() {    
    // 列表数据查询
    this.getList();

    var dictParams = [
    ];
  },
  methods: {
    // 查询数据
    getList() {
      this.loading = true;
      listCorecmsbillrefund(this.queryParams).then(res => {
         if (res.code == 200) {
           this.dataList = res.data.result;
           this.total = res.data.totalNum;
           this.loading = false;
         }
       })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 重置数据表单
    reset() {
      this.form = {
        refundId: undefined,
        aftersalesId: undefined,
        money: undefined,
        userId: undefined,
        sourceId: undefined,
        type: undefined,
        paymentCode: undefined,
        tradeNo: undefined,
        status: undefined,
        memo: undefined,
        createTime: undefined,
        updateTime: undefined,
        agentMoney: undefined,
        pId: undefined,
      };
      this.resetForm("form");
    },
    // 重置查询操作
    resetQuery() {
      this.timeRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.refundId);
      this.single = selection.length != 1
      this.multiple = !selection.length;
    },
     // 自定义排序
    sortChange(column) {
      if (column.prop == null || column.order == null) {
        this.queryParams.sort = undefined;
        this.queryParams.sortType = undefined;
      } else {
        this.queryParams.sort = column.prop;
        this.queryParams.sortType = column.order;
      }

      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
      this.opertype = 1;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.refundId || this.ids;

      this.$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？')
        .then(function () {
          return delCorecmsbillrefund(Ids);
        })
        .then(() => {
          this.handleQuery();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.refundId || this.ids;
      getCorecmsbillrefund(id).then((res) => {
        const { code, data } = res;
        if (code == 200) {
          this.open = true;
          this.title = "修改数据";
          this.opertype = 2;

          this.form = {
            ...data,
          };
        }
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.refundId != undefined && this.opertype === 2) {
            updateCorecmsbillrefund(this.form)
              .then((res) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
            })
          } else {
            addCorecmsbillrefund(this.form)
              .then((res) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
            })
          }
        }
      });
    },
  },
};
</script>